package com.xms.sdk.api.controller;

import com.xms.sdk.api.model.AssignedModel;
import com.xms.sdk.core.OwnerObject;
import com.xms.sdk.core.OwnerTypes;
import com.xms.sdk.service.IDataAssigner;
import com.xms.utils.CollectionUtil;
import com.xms.utils.UUIDUtil;
import com.xms.web.framework.controller.ApiBaseController;
import com.xms.web.framework.model.JsonResultObject;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * <p>
 * DataAssignerController
 * </p>
 *
 * @author migo
 * @since 2021/10/10
 */
@RestController
@RequestMapping("{org}/api/data")
public class DataAssignerController extends ApiBaseController {
    private IDataAssigner _dataAssigner;
    @Autowired
    public void DataAssignerController(IDataAssigner dataAssigner) {
        _dataAssigner = dataAssigner;
    }

    @ApiOperation("分派记录")
    @PostMapping("assign")
    public JsonResultObject assign(@RequestBody AssignedModel model) throws Exception {
        if (model == null || UUIDUtil.isEmpty(model.EntityId)
                || UUIDUtil.isEmpty(model.OwnerId) || CollectionUtil.isEmpty(model.ObjectId))
        {
            return JNotSpecifiedParams("所有者");
        }
        for (UUID item : model.ObjectId)
        {
            OwnerObject owner = null;
            if (model.OwnerIdType == 1) //assign to me
            {
                owner = new OwnerObject(OwnerTypes.SystemUser, _currentUser.getUser().SystemUserId);
            }
            else if (model.OwnerIdType == 2)
            {
                owner = new OwnerObject(OwnerTypes.SystemUser, model.OwnerId);
            }
            else if (model.OwnerIdType == 3)
            {
                owner = new OwnerObject(OwnerTypes.Team, model.OwnerId);
            }
            _dataAssigner.assign(model.EntityId, item, owner);
        }
        return JOk("分派成功");
    }

    @ApiOperation("分派记录")
    @PostMapping("assignuserrecords")
    public JsonResultObject assignUserRecords(){
        return JOk();
    }
}
